package com.dict.dao;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Repository;

@Repository
public class DictionaryDaoImpl extends BaseDao implements DictionaryDao {

	@Override
	public Map<String, Object> getDictionaryById(int id) {
		String sql = "select * from t_dict where id = ?";
		return this.getJdbcTemplate().queryForMap(sql, id);
	}

	@Override
	public List<Map<String, Object>> getDictionaryChild(int pid) {
		String pathReg = "%;" + pid + ";";
		String sql = "select * from t_dict where path like ? order by sort asc";
		return this.getJdbcTemplate().queryForList(sql, pathReg);
	}

	@Override
	public Map<String, Object> getDictionaryByName(String name) {
		String sql = "select * from t_dict where name = ?";
		return this.getJdbcTemplate().queryForMap(sql, name);
	}

	private Object[] toArray(Map<String, String> dict) {
		String[] params = { "name", "value", "path", "sort", "remark" };
		Object[] args = new Object[params.length];
		for (int i = 0; i < args.length; i++) {
			String value = dict.get(params[i]);
			args[i] = value.equals("") ? null : value;
			//args[i] = value;
		}

		return args;
	}

	@Override
	public Map<String, Object> saveDictionary(Map<String, String> dict) {
		String sql = "insert into t_dict(name, value, path, sort, remark) values(?, ?, ?, ?, ?)";
		Object[] args = toArray(dict);

		this.getJdbcTemplate().update(sql, args);

		return getDictionaryByName((String) dict.get("name"));
	}

	@Override
	public Map<String, Object> updateDictionary(Map<String, String> dict) {
		String sql = "update t_dict set name = ?, value = ?, path = ?, sort = ?, remark = ? where id = ?";
		Object[] value = toArray(dict);
		System.out.println(Arrays.toString(value));
		Object[] args = new Object[value.length + 1];
		for (int i = 0; i < value.length; i++) {
			args[i] = value[i];
		}
		args[value.length] = dict.get("id");

		System.out.println(Arrays.toString(args));
		this.getJdbcTemplate().update(sql, args);

		return getDictionaryByName((String) dict.get("name"));
	}

	@Override
	public List<Map<String, Object>> removeDictionaryTree(int id) {
		List<Map<String, Object>> list = getDictionaryTree(id);

		String sql = "delete from t_dict where id = ? or path like ?";
		String pathReg = getPathReg(id);
		this.getJdbcTemplate().update(sql, id, pathReg);
		
		return list;
	}
	
	private List<Map<String, Object>> getDictionaryTree(int id) {
		String sql = "select * from t_dict where id = ? or path like ?";
		String pathReg = getPathReg(id);
		
		return this.getJdbcTemplate().queryForList(sql, id, pathReg);
	}
	
	private String getPathReg(int id) {
		return "%;" + id + ";%";
	}

	@Override
	public boolean hasChild(int id) {		
		String sql = "select count(*) from t_dict where path like ?";
		int size = this.getJdbcTemplate().queryForObject(sql, Integer.class, getPathReg(id));
		
		return size > 0;
	}

	@Override
	public int getRows() {
		String sql = "select count(*) from t_dict where path = ?";
		return this.getJdbcTemplate().queryForObject(sql, Integer.class, ";0;");
	}

	@Override
	public List<Map<String, Object>> getDictionary(int page, int rows) {
		String sql = "select * from ( select * from t_dict where path = ? order by name asc) temp limit ?, ?";
		int offset = (page - 1) * rows;
		return this.getJdbcTemplate().queryForList(sql, ";0;", offset, rows);
	}

	@Override
	public boolean isExist(String name) {
		String sql = "select count(*) from t_dict where name = ?";
		return this.getJdbcTemplate().queryForObject(sql, Integer.class, name) > 0;
	}
	
	public boolean isExist(int id) {
		String sql = "select count(*) from t_dict where id = ?";
		return this.getJdbcTemplate().queryForObject(sql, Integer.class, id) == 1;
	}

	@Override
	public List<Map<String, Object>> getDictionary(String name, String value) {
		String sql = "select * from t_dict where " + name + " like ? order by name asc";
		String param = "%" + value + "%";
		return this.getJdbcTemplate().queryForList(sql, param);
	}
	
	private int getNextSort(String path) {
		String sql = "select max(sort) from t_dict where path = ?";
		Integer max = this.getJdbcTemplate().queryForObject(sql, Integer.class, path);
		return max == null ? 0 : max.intValue() + 1;
	}
	
	private List<Map<String, Object>> move(int id, String path, int oldSort, int newSort) {
		String sql = "select * from t_dict where path = ? and sort in (?, ?)";
		List<Map<String, Object>> list = this.getJdbcTemplate().queryForList(sql, path, oldSort, newSort);
		
		int size = 2;
		if (list.size() != size) {
			return null;
		}
		
		System.out.println("old------" + list);
		// 调整顺序
		for (int i = 0; i < size; i++) {
			Map<String, Object> temp = list.get(i);
			Integer tempId = (Integer) temp.get("id");
			if (tempId.intValue() == id) {
				temp.put("sort", newSort);
			} else {
				temp.put("sort", oldSort);
			}
		}
		System.out.println("new--------" + list);
		
		// 保存结果
		String updateSql = "update t_dict set sort = ? where id = ?";
		List<Object[]> batchArgs = new ArrayList<Object[]>();
		for (Map<String, Object> i : list) {
			batchArgs.add(new Object[]{ i.get("sort"), i.get("id") });
		}
		
		this.getJdbcTemplate().batchUpdate(updateSql, batchArgs);
		
		return list;		
	}

	@Override
	public List<Map<String, Object>> moveUp(int id, String path, int sort) {
		return move(id, path, sort, sort - 1);
	}

	@Override
	public List<Map<String, Object>> moveDown(int id, String path, int sort) {
		return move(id, path, sort, sort + 1);
	}
}
